//
//  ShopTableViewController.m
//  jianpai
//
//  Created by yrapp on 15/12/31.
//  Copyright © 2015年 yrapp. All rights reserved.
//

#import "CartTableViewController.h"
#import "ShopTableViewCell.h"
#import "ShopFootView.h"
#import "ShopModel.h"

#import "ShopOrderViewController.h"

//购物车点击商品 跳转webView
#import "ShopDetailViewController.h"



@interface CartTableViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIAlertViewDelegate>
@property (nonatomic,strong)ShopFootView *footView;
@property (nonatomic,strong)UITableView *tableView;
//每个商品是否选择 的标记
@property (nonatomic,strong)NSMutableArray *markArray;
//商品数量(防 复用)
@property (nonatomic,strong)NSMutableArray *numItemArray;
//商品ID 和商品数量配合使用
@property (nonatomic,strong)NSMutableArray *numIDArray;
@property (nonatomic,strong)UIBarButtonItem *rightButton;
@property (nonatomic,strong)NSMutableArray *shopArray;

@property (nonatomic,strong)AppDelegate *delegate;
@property (nonatomic,assign)BOOL editBool;
//没有商品时
@property (nonatomic,strong)UIView *noShopView;

//若没有登录 前往登录
@property (nonatomic,strong)UIAlertView *loginAlertView;


@end

@implementation CartTableViewController


/*
 tag 300  全选按钮
 tag 1000+  单个商品选择按钮
 tag 2000+  商品数量 textField
 tag 3000+  加号按钮
 tag 4000+  减号按钮
 */

- (void)viewWillAppear:(BOOL)animated {
    
    //解析数据
    [self updataForShop];
    
    self.footView.allPriceLabel.text = [NSString stringWithFormat:@"总计: 0元"];
    self.footView.selectButton.selected = NO;
    [self.footView.selectButton setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
    
    
    self.navigationController.navigationBarHidden = NO;
    
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:242.0/255 alpha:1];
    
    self.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.navigationItem.title = @"购物车";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,nil]];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    
    self.rightButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(edit)];
    
    self.navigationItem.rightBarButtonItem = self.rightButton;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     
                                                                     [UIColor whiteColor], UITextAttributeTextColor,
                                                                     
                                                                     nil]];

    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backButton setImage:[UIImage imageNamed:@"icon_fh"] forState:(UIControlStateNormal)];
    backButton.frame = CGRectMake(0, 0, 32, 32);
    [backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(6, 0, 6, 20);
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftButton;

    
#pragma mark --- 添加tableView ---
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 44) style:(UITableViewStylePlain)];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.backgroundColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:242.0/255 alpha:1];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.tableView registerClass:[ShopTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.loginAlertView = [[UIAlertView alloc] initWithTitle:@"未登录" message:@"请前往个人中心进行登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    self.loginAlertView.delegate = self;
    
    //解析数据
    [self updataForShop];
    
    //添加尾视图
    [self addFootView];
    
    //添加没有商品的购物车
    [self addNoShopView];

}
//AlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
//    if (buttonIndex == 0) {
//        LoginViewController *LoginVC = [[LoginViewController alloc] init];
//        [self presentViewController:LoginVC animated:YES completion:nil];
//    }
    
}
// 返回按钮
- (void)back {
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//编辑按钮
- (void)edit {
    
    
    _editBool = !_editBool;
    
    if (_editBool) {
    self.rightButton.title = @"完成";
        
        _footView.allPriceLabel.hidden = !_footView.allPriceLabel.hidden;
        _footView.clearButton.hidden = !_footView.clearButton.hidden;
        _footView.privilegeLabel.hidden = !_footView.privilegeLabel.hidden;
        _footView.shareButton.hidden = !_footView.shareButton.hidden;
        _footView.collectButton.hidden = !_footView.collectButton.hidden;
        _footView.removeButton.hidden = !_footView.removeButton.hidden;
        
    }else {
        
        self.rightButton.title = @"编辑";
        _footView.allPriceLabel.hidden = !_footView.allPriceLabel.hidden;
        _footView.clearButton.hidden = !_footView.clearButton.hidden;
        _footView.privilegeLabel.hidden = !_footView.privilegeLabel.hidden;
        _footView.shareButton.hidden = !_footView.shareButton.hidden;
        _footView.collectButton.hidden = !_footView.collectButton.hidden;
        _footView.removeButton.hidden = !_footView.removeButton.hidden;

    }
    
    NSLog(@"编辑");
}


#pragma mark -- 数据处理 ---
- (NSMutableArray *)numIDArray {
    if (!_numIDArray) {
        _numIDArray = [NSMutableArray array];
    }
    return _numIDArray;
}

- (NSMutableArray *)numItemArray {
    
    if (!_numItemArray) {
        _numItemArray = [NSMutableArray array];
    }
    return _numItemArray;
}
- (NSMutableArray *)markArray {
    
    if (!_markArray) {
        _markArray = [NSMutableArray array];
    }
    return _markArray;
}
- (NSMutableArray *)shopArray {
    if (!_shopArray) {
        _shopArray = [NSMutableArray array];
    }
    return _shopArray;
}


//数据解析
- (void)updataForShop {
    
   //NSString *uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFUUID"];
    NSString *url = [KShopUrl stringByAppendingString:[NSString stringWithFormat:@"&uuid=5f7fba50d937521e708bde16edb095e62e9f9e49"]];
    
    
    NSLog(@"购物车 %@",url);
    
    [LORequestManger GET:url success:^(id response) {
        
        [self.markArray removeAllObjects];
        [self.numItemArray removeAllObjects];
        [self.shopArray removeAllObjects];
        
        NSDictionary *dic = (NSDictionary *)response;
        
        NSArray *array = dic[@"data"];
        
        
        if (array.count == 0) {
            self.noShopView.hidden = NO;
        }else {
            self.noShopView.hidden = YES;
            
        }
        
        for (NSDictionary *item in array) {
            
            ShopModel *model = [ShopModel setUpdateDic:item];
            [self.markArray addObject:@"0"];
            [self.numItemArray addObject:model.num];
            [self.numIDArray addObject:[model.gourl substringWithRange:NSMakeRange(58, 5)]];
            [self.shopArray addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


#pragma mark --- 页面 ---
//没商品时显示的图标
- (void)addNoShopView {
    //背景界面
    self.noShopView = [[UIView alloc] initWithFrame:CGRectMake(0, 74, kWidth, kHeight - 64-10-44)];
    self.noShopView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.noShopView];
    //购物车图片
    UIImageView *shopImage = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth - 100)/2,80.0/667 * kWidth,100, 100)];
    shopImage.image = [UIImage imageNamed:@"购物车"];
    [self.noShopView addSubview:shopImage];
    //提示文字
    UILabel *gouwuLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,shopImage.frame.origin.y + shopImage.frame.size.height + 22, kWidth, 30)];
    gouwuLabel.text = @"亲~您的购物车还没有商品哦,快去挑选吧!";
    gouwuLabel.textColor = [UIColor colorWithRed:156/255.0 green:156.0/255 blue:156.0/255 alpha:1];
    gouwuLabel.font = [UIFont systemFontOfSize:14];
    gouwuLabel.textAlignment = NSTextAlignmentCenter;
    [self.noShopView addSubview:gouwuLabel];
    //去看看
    UIButton *goButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    goButton.frame = CGRectMake((kWidth - 125) / 2, gouwuLabel.frame.origin.y + 50, 125, 44);
    goButton.layer.masksToBounds = YES;
    goButton.layer.cornerRadius = 5;
    [goButton setTitle:@"去看看~" forState:(UIControlStateNormal)];
    [goButton addTarget:self action:@selector(noShop) forControlEvents:(UIControlEventTouchUpInside)];
    goButton.backgroundColor = [UIColor colorWithRed:202.0/255 green:33.0/255 blue:41.0/255 alpha:1];
    [self.noShopView addSubview:goButton];
}

- (void)noShop {
    
    NSLog(@"没有商品");
    
    [self.tabBarController setSelectedIndex:0];
    
}


- (void)addFootView {
    _footView = [[ShopFootView alloc] initWithFrame:CGRectMake(0, kHeight - 92, kWidth, 45)];
    _footView.backgroundColor = [UIColor colorWithRed:228.0/255 green:235.0/255 blue:239.0/255 alpha:1];
    //_footView.backgroundColor = [UIColor grayColor];
    
    if (self.navigationController.viewControllers.count > 1) {
        _footView.frame = CGRectMake(0, kHeight - 45, kWidth, 45);
    }
    
    [_footView.selectButton addTarget:self action:@selector(allSelectButton:) forControlEvents:(UIControlEventTouchUpInside)];
    _footView.selectButton.tag = 100;
    
    
    [_footView.clearButton addTarget:self action:@selector(jiesuanButton) forControlEvents:(UIControlEventTouchUpInside)];
    [_footView.shareButton addTarget:self action:@selector(fenxiangButton) forControlEvents:(UIControlEventTouchUpInside)];
    [_footView.collectButton addTarget:self action:@selector(shoucangButton) forControlEvents:(UIControlEventTouchUpInside)];
    [_footView.removeButton addTarget:self action:@selector(shanchuButton) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.view addSubview:_footView];
    
}

#pragma mark --- footView Button ---
//结算按钮
- (void)jiesuanButton {
    NSLog(@"结算");
    
    int j = 0;
    for (int i = 0; i < self.markArray.count; i++) {
        
        if ([self.markArray[i] isEqualToString:@"1"]) {
            j++;
        }
        
    }
    
    //商品id
    NSString *idStr;
    for (int i = 0; i < self.markArray.count; i++) {
        if ([self.markArray[i] isEqualToString:@"1"]) {
            if ([idStr length] == 0) {
                idStr = [NSString stringWithFormat:@"%@",self.numIDArray[i]];
            }else {
                idStr =  [idStr stringByAppendingString:[NSString stringWithFormat:@",%@",self.numIDArray[i]]];
            }
        }
    }
    
    //选中项目j大于0 可以进入结算界面
    if (j > 0) {
        
        //判断是否登陆了
        if (self.delegate.isLogin || self.delegate.isThirdLogin || 1) {
            
            NSString *cuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFUUID"];
            NSString *url = [KJieSuan stringByAppendingString:[NSString stringWithFormat:@"&uuid=%@&cpid_list=%@",cuid,idStr]];
            NSLog(@"结算的 Url %@",url);
            [LORequestManger GET:url success:^(id response) {
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
            
            NSArray *array = self.navigationController.viewControllers;
            NSLog(@" 我擦啊 %lu",(unsigned long)array.count);
            ShopOrderViewController *shopOrderVC = [[ShopOrderViewController alloc] init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:shopOrderVC animated:YES];
            if (array.count > 1) {
            }else {
                self.hidesBottomBarWhenPushed = NO;
            }
            
        }else {
            [self.loginAlertView show];
        }
        
    }else {
        
        NSLog(@"请选择商品");
        
    }
}
//分享按钮
- (void)fenxiangButton {
    
    int j = 0;
    for (int i = 0; i < self.markArray.count; i++) {
        
        if ([self.markArray[i] isEqualToString:@"1"]) {
            j++;
        }
    }
    
    if (j > 0) {
        NSLog(@"分享");
    }else {
        
        NSLog(@"请选择商品");
        
    }
}
//收藏按钮
- (void)shoucangButton {
    NSLog(@"收藏");
}
//移除按钮
- (void)shanchuButton {
    
    for (int i = 0; i < self.markArray.count; i++) {
        
        if ([self.markArray[i] isEqualToString:@"1"]) {
            
            
            [self.numItemArray replaceObjectAtIndex:i withObject:@"0"];
            
        }
    }
    
    //商品id
    NSString *idStr;
    //商品数量
    NSString *numStr;
    for (int i = 0; i < self.markArray.count; i++) {
        if ([self.markArray[i] isEqualToString:@"1"]) {
            if ([idStr length] == 0) {
                idStr = [NSString stringWithFormat:@"%@",self.numIDArray[i]];
                numStr = [NSString stringWithFormat:@"%@",self.numItemArray[i]];
            }else {
                idStr =  [idStr stringByAppendingString:[NSString stringWithFormat:@",%@",self.numIDArray[i]]];
                numStr = [numStr stringByAppendingString:[NSString stringWithFormat:@",%@",self.numItemArray[i]]];
            }
        }
    }
    
    NSString *uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFUUID"];
    
    NSString *url = [KShopEdUrl stringByAppendingString:[NSString stringWithFormat:@"&uuid=%@&cpid_list=%@&num_list=%@",uuid,idStr,numStr]];
    
    NSLog(@"******%@",url);
    [LORequestManger GET:url success:^(id response) {
        [self updataForShop];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    [self allPriceForMark];
    
    

    UIButton *allButton = (UIButton *)[self.view viewWithTag:100];
    allButton.selected = NO;
    [allButton setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.shopArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //商品数量
    cell.numField.delegate = self;
    cell.numField.tag = 2000 + indexPath.row;
    if (self.numItemArray.count > 0) {
        cell.numField.text = self.numItemArray[indexPath.row];
    }
    
    //加号按钮
    cell.addButton.tag = 3000 + indexPath.row;
    [cell.addButton addTarget:self action:@selector(addNumButton:) forControlEvents:(UIControlEventTouchUpInside)];
    //减号按钮
    cell.deleButton.tag = 4000 + indexPath.row;
    [cell.deleButton addTarget:self action:@selector(deleNumButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //商品选择按钮
    if ([self.markArray[indexPath.row] isEqualToString:@"0"]) {
        [cell.selectButtton setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
    }else {
        [cell.selectButtton setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
    }
    [cell.selectButtton addTarget:self action:@selector(selectButtonItem:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.selectButtton.tag = 1000 + indexPath.row;
    
    
    ShopModel *model = self.shopArray[indexPath.row];
    //赋值测试用
    [cell setModel:model];

    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 114;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShopDetailViewController *webVC = [[ShopDetailViewController alloc] init];

    if (self.navigationController.viewControllers.count == 1) {
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else {
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];

    }

}

#pragma mark --- 商品数量系列问题 ---
//计算商品总价格
- (void)allPriceForMark {
    
    int allPrice = 0;
    
    for (int i = 0; i < self.markArray.count; i++) {
        if ([self.markArray[i] isEqualToString:@"1"]) {
            
            ShopModel *model = self.shopArray[i];
    
            int num = [self.numItemArray[i] intValue];
            int ID = [model.price intValue];
            
            allPrice = allPrice + num * ID;
            
        }
    }
    
    self.footView.allPriceLabel.text = [NSString stringWithFormat:@"总计: %d元",allPrice];
    
}


//编辑数量变成选中状态
- (void)selectBtutton:(int)index {
    
    UIButton *button = (UIButton *)[self.tableView viewWithTag:index + 1000];
    
    
    [self.markArray replaceObjectAtIndex:index withObject:@"1"];
    [button setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
    
    
    //根据单个商品状态关联 全选
    int j = 0;
    for (int i = 0;i < self.markArray.count; i++) {
        if ([self.markArray[i] isEqualToString:@"0"]) {
            j++;
        }
    }
    if (j == 0) { //全部选中
        UIButton *allButton = (UIButton *)[self.view viewWithTag:100];
        allButton.selected = YES;
        [allButton setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
    }else {
        UIButton *allButton = (UIButton *)[self.view viewWithTag:100];
        allButton.selected = NO;
        [allButton setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
    }

    
    [self endShopEdit];
    
}
//编辑完 网络请求数据
- (void)endShopEdit {
    
    //商品id
    NSString *idStr;
    //商品数量
    NSString *numStr;
    
    for (int i = 0; i < self.markArray.count; i++) {
        
        
        
        if ([self.markArray[i] isEqualToString:@"1"]) {
            
            if ([idStr length] == 0) {
                
                idStr = [NSString stringWithFormat:@"%@",self.numIDArray[i]];
                numStr = [NSString stringWithFormat:@"%@",self.numItemArray[i]];
            }else {
                idStr =  [idStr stringByAppendingString:[NSString stringWithFormat:@",%@",self.numIDArray[i]]];
                numStr = [numStr stringByAppendingString:[NSString stringWithFormat:@",%@",self.numItemArray[i]]];

            }
            
            
        }
    }
    
    NSString *uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFUUID"];

    NSString *url = [KShopEdUrl stringByAppendingString:[NSString stringWithFormat:@"&uuid=%@&cpid_list=%@&num_list=%@",uuid,idStr,numStr]];
    
    NSLog(@"******%@",url);
    [LORequestManger GET:url success:^(id response) {
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    [self allPriceForMark];
    
}


//+
- (void)addNumButton:(UIButton *)button {
    
    int index = button.tag - 3000;
    UITextField *textField = (UITextField *)[self.tableView viewWithTag:index + 2000];
    int value = [textField.text intValue];
    ++value;
    
    textField.text = [NSString stringWithFormat:@"%d",value];
    [self.numItemArray replaceObjectAtIndex:index withObject:textField.text];
    
    [self selectBtutton:index];
}
//-
- (void)deleNumButton:(UIButton *)button {
    
    int index = button.tag - 4000;
    UITextField *textField = (UITextField *)[self.tableView viewWithTag:index + 2000];
    int value = [textField.text intValue];
    --value;
    if (value < 1) {
        value = 1;
    }
    textField.text = [NSString stringWithFormat:@"%d",value];
    [self.numItemArray replaceObjectAtIndex:index withObject:textField.text];

    [self selectBtutton:index];

}

//输入框 数量限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  
        NSLog(@"%ld",(long)textField.tag);
    
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if ([toBeString length] > 3)
        {
            textField.text = [toBeString substringToIndex:3];
            
          //  [self.numItemArray replaceObjectAtIndex:textField.tag - 2000 withObject:textField.text];
            
            return NO;
        }if ([toBeString length] == 0) {
           // textField.text = @"1";
           // [self.numItemArray replaceObjectAtIndex:textField.tag - 2000 withObject:textField.text];
           // return NO;
        }
    
    NSLog(@"keng  %@",textField.text);
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.numItemArray replaceObjectAtIndex:textField.tag - 2000 withObject:textField.text];
    
    
    if ([textField.text length] == 0) {
        textField.text = @"1";
    }
    
    if ([textField.text intValue] < 1) {
        textField.text = @"1";
    }
    
    [self selectBtutton:textField.tag - 2000];

    NSLog(@"%ld",(long)textField.tag);
    
    
}

//选中单个商品 操作
- (void)selectButtonItem:(UIButton *)button {
    
    int index = button.tag - 1000;
    
    //单个商品是否选中
    if ([self.markArray[index] isEqualToString:@"0"]) {
        [button setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
        [self.markArray replaceObjectAtIndex:index withObject:@"1"];
    }else {
        [button setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
        [self.markArray replaceObjectAtIndex:index withObject:@"0"];
    }
    
    //根据单个商品状态关联 全选
    int j = 0;
    for (int i = 0;i < self.markArray.count; i++) {
        if ([self.markArray[i] isEqualToString:@"0"]) {
            j++;
        }
    }
    if (j == 0) { //全部选中
        UIButton *allButton = (UIButton *)[self.view viewWithTag:100];
        allButton.selected = YES;
        [allButton setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
    }else {
        UIButton *allButton = (UIButton *)[self.view viewWithTag:100];
        allButton.selected = NO;
        [allButton setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
    }
    
    [self endShopEdit];

}

//选中全部商品
- (void)allSelectButton:(UIButton *)button {
    
    if (self.shopArray.count > 0) {
        
        button.selected = !button.selected;
        
        if (button.selected) {
            
            [button setImage:[UIImage imageNamed:@"check_box_on@2x"] forState:(UIControlStateNormal)];
            
            for (int i = 0; i < self.markArray.count; i++) {
                
                [self.markArray replaceObjectAtIndex:i withObject:@"1"];
                
            }
            
        }else {
            
            [button setImage:[UIImage imageNamed:@"check_box@2x"] forState:(UIControlStateNormal)];
            for (int i = 0; i < self.markArray.count; i++) {
                
                [self.markArray replaceObjectAtIndex:i withObject:@"0"];
                
            }
        }
        [self endShopEdit];
        
        [self.tableView reloadData];
    
    }

    
}

#pragma mark --- 自定义数字键盘---
//滑动 回收键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

@end
